<?php
include 'checklogin.php';
$act=daddslashes($_GET["act"]);
switch ($act){
    case 'setsys':
        if (isset($_POST["submit"])){
            $updates=[
                "title"=>daddslashes($_POST["title"]),
                "keywords"=>daddslashes($_POST["keywords"]),
                "description"=>daddslashes($_POST["description"]),
                "logo"=>daddslashes($_POST["logo"]),
                "qq"=>daddslashes($_POST["qq"]),
                "aounce"=>daddslashes($_POST["aounce"]),
                "shaounce"=>daddslashes($_POST["shaounce"]),
                "ip"=>daddslashes($_POST["ip"]),
                "footer"=>daddslashes($_POST["footer"]),
                 "gybz"=>daddslashes($_POST["gybz"]),
                 "token"=>daddslashes($_POST["token"])
            ];
            try {
                $sql="UPDATE 9g_index SET `title`='{$updates["title"]}',`keywords`='{$updates["keywords"]}',`description`='{$updates["description"]}',`qq`='{$updates["qq"]}',`logo`='{$updates["logo"]}',`aounce`='{$updates["aounce"]}',`shaounce`='{$updates["shaounce"]}',`footer`='{$updates["footer"]}',`ip`='{$updates["ip"]}',`gybz`='{$updates["gybz"]}',`token`='{$updates["token"]}' WHERE id='1'";
                $update=$pdo->exec($sql);
                if ($update>=1){
                    $state=1;
                }else{
                    $state=0;
                }
            }catch (Exception $e){
                $state=0;
            }
            $pdo=null;
            $update=null;
            // $update=null;
            die(json_encode(["state"=>$state]));
        }
        break;
        case 'setemail':
            if (isset($_POST["submit"])){
                $updates=[
                    "emailurl"=>daddslashes($_POST["url"]),
                    "emailport"=>daddslashes($_POST["port"]),
                    "emailuser"=>daddslashes($_POST["user"]),
                    "emailpass"=>daddslashes($_POST["pass"]),
                ];
                try {
                    $sql="UPDATE 9g_index SET `emailurl`='{$updates["emailurl"]}',`emailport`='{$updates["emailport"]}',`emailuser`='{$updates["emailuser"]}',`emailpass`='{$updates["emailpass"]}' WHERE id='1'";
                    $update=$pdo->exec($sql);
                    if ($update>=1){
                        $state=1;
                    }else{
                        $state=0;
                    }
                }catch (Exception $e){
                    $state=0;
                }
                $pdo=null;
                $update=null;
                // $update=null;
                die(json_encode(["state"=>$state]));
            }
            break;
    case 'setpass':
        if (isset($_POST["username"]) && isset($_POST["password"])) {
            $username = daddslashes($_POST["username"]);
            $password = daddslashes($_POST["password"]);
            try {
                    $updatepass = $pdo->exec("UPDATE 9g_index SET `password`='{$password}',`username`='{$username}' WHERE `id`='1'");
                    if($updatepass>=1){
                        setcookie("username","",time()-3600*72);
                    $state = 1;
                } else {
                    $state = 2;
                }
            } catch (Exception $e) {
                $state = 0;
            }
            $pdo=null;
            $updatepass=null;
            die(json_encode(["state" => $state]));
        }
        break;
    case 'adduser':
        if (isset($_POST["username"]) && isset($_POST["password"])){
            $username=daddslashes($_POST["username"]);
            $password=daddslashes($_POST["password"]);
            $qq=daddslashes($_POST["qq"]);
            $date=date('Y-m-d h:i:s', time());
            $ip=real_ip();
            try {
                if ($pdo->exec("INSERT INTO 9g_user(`username`,`password`,`qq`,`time`,`status`) VALUES('{$username}','{$password}','{$qq}','{$date}','1')")>=1){
                    $state=1;
                }else{ 
                    $state=0;
                }
            }catch (Exception $e){
                $state=0;
            }
            $pdo=null;
            die(json_encode(["state"=>$state]));
        }
        break;
    case 'addyou':
        if (isset($_POST["name"]) && isset($_POST["url"]) && isset($_POST["favicon"])){
            $favicon=daddslashes($_POST["favicon"]);
            $name=daddslashes($_POST["name"]);
            $url=daddslashes($_POST["url"]);
            $ip=real_ip();
            $time=date('Y-m-d h:i:s', time());
            try {
                if ($pdo->exec("INSERT INTO 9g_you(`name`,`favicon`,`url`,`status`,`ip`,`time`) VALUES('{$name}','{$favicon}','{$url}','1','{$ip}','{$time}')")>=1){
                    $state=1;
                }else{
                    $state=0;
                }
            }catch (Exception $e){
                $state=0;
            }
            $pdo=null;
            die(json_encode(["state"=>$state]));
        }
        break;
    case 'upyou':
        if (isset($_POST["name"]) && isset($_POST["url"]) && isset($_POST["favicon"]) && isset($_POST["id"])){
            $favicon=daddslashes($_POST["favicon"]);
            $name=daddslashes($_POST["name"]);
            $url=daddslashes($_POST["url"]);
            $id=daddslashes($_POST["id"]);
            if ($pdo->exec("UPDATE 9g_you SET `favicon`='{$favicon}',`name`='{$name}',`url`='{$url}' WHERE `id`='{$id}'")>=1){
                $state=1;
            }else{
                $state=0;
            }
            $pdo=null;
            die(json_encode(["state"=>$state]));
        }
        break;
                case 'delyou':
            $id=daddslashes($_POST["id"]);
            if (isset($id)){
                    if ($pdo->exec("delete from 9g_you where id='{$id}'")>=1){
                        $state=1;
                    }
                    $pdo=null;
                    exit(json_encode(["state"=>$state]));
                }
            break;
        case 'delsyou':
            if (isset($_POST["act"])){
                if($pdo->exec("DELETE FROM 9g_you")>=1){
                    $state=1;
                }
                $pdo=null;
                exit(json_encode(["state"=>$state]));
            }
          
            break;
    case 'checkuser':
        if (isset($_POST["user"])){
            $data=[];
            $user=daddslashes($_POST["user"]);
            try {
                $query=$pdo->query("SELECT * FROM 9g_info where user={$user}");
                if ($query->rowCount()>=1){
                    // $data=pdo_data($query);
                    $status=1;
                }else{
                    $status=0;
                }
            }catch (Exception $e){
                $status=0;
            }
            die(json_encode(["status"=>$status,"id"=>$data["id"],"user"=>$data["user"],"user"=>$data["user"],"datetime"=>date('Y-m-s h:i:s',$data["datetime"]),"ip"=>get_ip_city($data["ip"])]));
        }
        break;
    case 'upuser':
        if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["id"])  && isset($_POST["qq"])){
            $username=daddslashes($_POST["username"]);
            $password=daddslashes($_POST["password"]);
            $qq=daddslashes($_POST["qq"]);
            $id=daddslashes($_POST["id"]);
            if ($pdo->exec("UPDATE 9g_user SET `username`='{$username}',`password`='{$password}',`qq`='{$qq}' WHERE `id`='{$id}'")>=1){
                $state=1;
            }else{
                $state=0;
            }
            $pdo=null;
            die(json_encode(["state"=>$state]));
        }
        break;
        case 'deluser':
            $id=daddslashes($_POST["id"]);
            if (isset($id)){
                    if ($pdo->exec("delete from 9g_user where id='{$id}'")>=1){
                        $state=1;
                    }
                    $pdo=null;
                    exit(json_encode(["state"=>$state]));
                }
            break;
        case 'delsuser':
            if (isset($_POST["act"])){
                if($pdo->exec("DELETE FROM 9g_user")>=1){
                    $state=1;
                }
                $pdo=null;
                exit(json_encode(["state"=>$state]));
            }
          
            break;
            case 'addtype':
                if (isset($_POST["type"])){
                    $type=daddslashes($_POST["type"]);
                    $ip=real_ip();
                    try {
                        if ($pdo->exec("INSERT INTO 9g_type(`type`) VALUES('{$type}')")>=1){
                            $state=1;
                        }else{ 
                            $state=0;
                        }
                    }catch (Exception $e){
                        $state=0;
                    }
                    $pdo=null;
                    die(json_encode(["state"=>$state]));
                }
                break;
            case 'uptype':
                if (isset($_POST["type"])){
                    $type=daddslashes($_POST["type"]);
                    $id=daddslashes($_POST["id"]);
                    if ($pdo->exec("UPDATE 9g_type SET `type`='{$type}' WHERE `id`='{$id}'")>=1){
                        $state=1;
                    }else{
                        $state=0;
                    }
                    $pdo=null;
                    die(json_encode(["state"=>$state]));
                }
                break;
                case 'deltype':
                    $id=daddslashes($_POST["id"]);
                    if (isset($id)){
                            if ($pdo->exec("delete from 9g_type where id='{$id}'")>=1){
                                $state=1;
                            }
                            $pdo=null;
                            exit(json_encode(["state"=>$state]));
                        }
                    break;
                case 'delstype':
                    if (isset($_POST["act"])){
                        if($pdo->exec("DELETE FROM 9g_type")>=1){
                            $state=1;
                        }
                        $pdo=null;
                        exit(json_encode(["state"=>$state]));
                    }
                  
                    break;
    case 'upload':
        include '../system/upload.php';
        $pic_path = "../assets/image";
        $uppic = $url . '/assets/image/';
        $upload = new upload($pic_path);
        if ($file_name = $upload->up($_FILES['file'])) {
            $status = 1;
            $id = $file_name;
            echo json_encode(['status' => 1, 'url' => $uppic.$id]);
            exit;
        } else {
            $status = 0;
            $msg = $upload->error();
            echo json_encode(['status' => 0, 'msg' => $msg]);
            exit;
        }
        break;
        case 'uploads':
            include '../system/upload.php';
            $pic_path = "../assets/image";
            $uppic = $url . '/assets/image/';
            $upload = new upload($pic_path);
            if ($file_name = $upload->up($_FILES['file'])) {
                $status = 1;
                $id = $file_name;
                echo json_encode(['status' => 1, 'location' => $uppic.$id]);
                exit;
            } else {
                $status = 0;
                $msg = $upload->error();
                echo json_encode(['status' => 0, 'msg' => $msg]);
                exit;
            }
            break;
        case 'addaounce':
            if (isset($_POST["title"]) && isset($_POST["aounce"]) && isset($_POST["image"])){
                $title=daddslashes($_POST["title"]);
                $aounce=daddslashes($_POST["aounce"]);
                $image=daddslashes($_POST["image"]);
                $date=date('Y-m-d h:i:s', time());
                try {
                    if ($pdo->exec("INSERT INTO 9g_aounce(`title`,`aounce`,`image`,`date`) VALUES('{$title}','{$aounce}','{$image}','{$date}')")>=1){
                        $state=1;
                    }else{ 
                        $state=0;
                    }
                }catch (Exception $e){
                    $state=0;
                }
                $pdo=null;
                die(json_encode(["state"=>$state]));
            }
            break;
            case 'upaounce':
                if (isset($_POST["title"]) && isset($_POST["aounce"]) && isset($_POST["image"])){
                    $title=daddslashes($_POST["title"]);
                    $aounce=daddslashes($_POST["aounce"]);
                    $image=daddslashes($_POST["image"]);
                    $id=daddslashes($_POST["id"]);
                    if ($pdo->exec("UPDATE 9g_aounce SET `title`='{$title}',`aounce`='{$aounce}',`image`='{$image}' WHERE `id`='{$id}'")>=1){
                        $state=1;
                    }else{
                        $state=0;
                    }
                    $pdo=null;
                    die(json_encode(["state"=>$state]));
                }
                break;
                case 'delaounce':
                    $id=daddslashes($_POST["id"]);
                    if (isset($id)){
                            if ($pdo->exec("delete from 9g_aounce where id='{$id}'")>=1){
                                $state=1;
                            $pdo=null;
                            exit(json_encode(["state"=>$state]));
                        }
                    }
                    break;
                case 'delsaounce':
                    if (isset($_POST["act"])){
                        if($pdo->exec("DELETE FROM 9g_aounce")>=1){
                            $state=1;
                        }
                        $pdo=null;
                        exit(json_encode(["state"=>$state]));
                    }
                  
                    break;
                    case 'addinfo':
                        if (isset($_POST["type"]) && isset($_POST["data"]) && isset($_POST["image"]) && isset($_POST["user"]) && isset($_POST["msg"])){
                            $type=daddslashes($_POST["type"]);
                            $data=daddslashes($_POST["data"]);
                            $image=daddslashes($_POST["image"]);
                            $user=daddslashes($_POST["user"]);
                            $msg=daddslashes($_POST["msg"]);
                            $date=date('Y-m-d h:i:s', time());
                            try {
                                if ($pdo->query("INSERT INTO 9g_msg(`type`,`data`,`image`,`user`,`msg`,`time`,`status`) VALUES('{$type}','{$data}','{$image}','{$user}','{$msg}','{$date}','1')")>=1){
                                        $query=$pdo->query("SELECT id,data FROM 9g_msg WHERE `data`='{$data}'")->fetch();
                                        if(isset($home["token"])){
                                        $check=json_decode(file_get_contents('http://api.9gyun.cn/BaiduApi/sl.php?url='.$url.'/'.$query["id"].'.html&root='.$_SERVER['HTTP_HOST'].'&token='.$home["token"]));
                                       
                                        if($check->success>=1){
                                            $pdo->exec("UPDATE 9g_msg SET `sl`='1' WHERE `id`='{$query["id"]}'");
                                        } }
                                    $state=1;
                                }else{ 
                                    $state=0;
                                }
                            }catch (Exception $e){
                                $state=0;
                            }
                            $pdo=null;
                            die(json_encode(["state"=>$state]));
                        }
                        break;
                        case 'upinfo':
                            if (isset($_POST["type"]) && isset($_POST["data"]) && isset($_POST["image"]) && isset($_POST["user"]) && isset($_POST["msg"])){
                                $type=daddslashes($_POST["type"]);
                                $data=daddslashes($_POST["data"]);
                                $image=daddslashes($_POST["image"]);
                                $user=daddslashes($_POST["user"]);
                                $msg=daddslashes($_POST["msg"]);
                                $id=daddslashes($_POST["id"]);
                                if ($pdo->exec("UPDATE 9g_msg SET `type`='{$type}',`data`='{$data}',`image`='{$image}',`user`='{$user}',`msg`='{$msg}' WHERE `id`='{$id}'")>=1){
                                    $state=1;
                                }else{
                                    $state=0;
                                }
                                $pdo=null;
                                die(json_encode(["state"=>$state]));
                            }
                            break;
                                case 'delinfo':
                                    $id=daddslashes($_POST["id"]);
                                    if (isset($id)){
                                            if ($pdo->exec("delete from 9g_msg where id='{$id}'")>=1){
                                                $state=1;
                                            }
                                            $pdo=null;
                                            exit(json_encode(["state"=>$state]));
                                        }
                                    break;
                                case 'delsinfo':
                                    if (isset($_POST["act"])){
                                        if($pdo->exec("DELETE FROM 9g_msg")>=1){
                                            $state=1;
                                        }
                                        $pdo=null;
                                        exit(json_encode(["state"=>$state]));
                                    }
                                  
                                    break;
                                case 'csemail':
                                    include '../system/impl_Mail.php';
                                    if (isset($_POST["submit"])){
                                        $updates=[
                                            "emailurl"=>daddslashes($_POST["url"]),
                                            "emailport"=>daddslashes($_POST["port"]),
                                            "emailuser"=>daddslashes($_POST["user"]),
                                            "emailpass"=>daddslashes($_POST["pass"]),
                                            "to"=>daddslashes($_POST["to"]),
                                        ];
                                        $state=0;
                                        //sendMail($to,$url,$user,$pass,$port,$title,$content)
                                        $sendmail=sendMail($updates["to"],$updates["emailurl"],$updates["emailuser"],$updates["emailpass"],$updates["emailport"],'测试','云风轻产品www.9gyun.cn');
                                        if ($sendmail==true){
                                            $state=1;
                                        }
                                        $pdo=null;
                                        $update=null;
                                        // $update=null;
                                        die(json_encode(["state"=>$state]));
                                    }
                                    break;
                                    case 'tgemail':
                                        include '../system/impl_Mail.php';
                                        if (isset($_POST["email"])){
                                            $email=daddslashes($_POST["email"]);
                                            $state=0;
                                            //sendMail($to,$url,$user,$pass,$port,$title,$content)
                                            $sendmail=sendMail($email,$home["emailurl"],$home["emailuser"],$home["emailpass"],$home["emailport"],$home["title"].'-审核成功通知','恭喜你，你在本站填写的收录信息被本站收录了-----'.$home["title"].$_SERVER['HTTP_HOST']);
                                            if ($sendmail==true){
                                                $state=1;
                                            }
                                            $pdo=null;
                                            $update=null;
                                            // $update=null;
                                            die(json_encode(["state"=>$state]));
                                        }
                                        break;
                                        case 'btgemail':
                                            include '../system/impl_Mail.php';
                                            if (isset($_POST["email"])){
                                                $email=daddslashes($_POST["email"]);
                                                $msg=daddslashes($_POST["msg"]);
                                                $state=0;
                                                //sendMail($to,$url,$user,$pass,$port,$title,$content)
                                                $sendmail=sendMail($email,$home["emailurl"],$home["emailuser"],$home["emailpass"],$home["emailport"],$home["title"].'-审核不通过通知','很抱歉，你在本站填写的收录信息有误，需要修改，驳回理由:'.$msg.'。请返回本站重新提交-----'.$home["title"].$_SERVER['HTTP_HOST']);
                                                if ($sendmail==true){
                                                    $state=1;
                                                }
                                                $pdo=null;
                                                $update=null;
                                                // $update=null;
                                                die(json_encode(["state"=>$state]));
                                            }
                                            break;
                                            case 'actinfo':
                                                if (isset($_POST["id"])){
                                                    $id=daddslashes($_POST["id"]);
                                                    if ($pdo->exec("UPDATE 9g_msg SET `status`='1' WHERE `id`='{$id}'")>=1){
                                                        $state=1;
                                                    }else{
                                                        $state=0;
                                                    }
                                                    $pdo=null;
                                                    die(json_encode(["state"=>$state]));
                                                }
                                                break;
                                                     case 'actsl':
                                                if (isset($_POST["id"])){
                                                    $id=daddslashes($_POST["id"]);
                                        $sl=json_decode(file_get_contents('http://api.9gyun.cn/BaiduApi/sl.php?url='.$url.'/page/'.$id.'.html&root='.$_SERVER['HTTP_HOST'].'&token='.$home["token"]));
                                        if($sl->success>=1){
                                          if ($pdo->exec("UPDATE 9g_msg SET `sl`='1' WHERE `id`='{$id}'")>=1){
                                                        $state=1;
                                                    }else{
                                                        $state=0;
                                                    }
                                        }else{
                                             $state=$sl->success;
                                        }
                                                   
                                                    $pdo=null;
                                                    die(json_encode(["state"=>$state]));
                                                }
                                                break;
                                                 case 'actyou':
                                                if (isset($_POST["id"])){
                                                    $id=daddslashes($_POST["id"]);
                                                    if ($pdo->exec("UPDATE 9g_you SET `status`='1' WHERE `id`='{$id}'")>=1){
                                                        $state=1;
                                                    }else{
                                                        $state=0;
                                                    }
                                                    $pdo=null;
                                                    die(json_encode(["state"=>$state]));
                                                }
                                                break;
                                                

            
        }
